package com.xkdx.guangguang.fragment.home;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONObject;

import com.xkdx.guangguang.module.network.AbsModule;
import com.xkdx.guangguang.shareclass.DiscountInfo;

public class HomeModule extends AbsModule {
	public List<HomeInfo> list;
	public List<DiscountInfo> nearInfoList;
	public List<Map<String, String>> cityList;
	JSONArray backJson;
	JSONObject jsonObj;

	@Override
	public void parseData() throws Exception {

		try {
			backJson = (JSONArray) result;
			nearInfoList = new ArrayList<DiscountInfo>();
			cityList = new ArrayList<Map<String, String>>();
			for (int i = 0; i < backJson.length(); i++) {
				jsonObj = (JSONObject) backJson.get(i);
				String sortOfAct = jsonObj.getString("Act");
				if (sortOfAct.equals("getUpdateHomeInfoList")) {
					list = parserHome(jsonObj.getJSONObject("Content"));
					nearInfoList = parseHomeDiscountInfo(jsonObj.getJSONObject("Content"));
				} else if (sortOfAct.equals("getCityList")) {
					cityList = parseCityList(jsonObj.getJSONObject("Content"));
				} else {
//					nearInfoList = parseHomeDiscountInfo(jsonObj.getJSONObject("Content"));
				}

			}
		} catch (Exception e) {
			// to do define a exception include all parseException
			throw e;
		}

	}

	private List<Map<String, String>> parseCityList(JSONObject person) throws Exception {

		List<Map<String, String>> list = new ArrayList<Map<String, String>>();
		try {
			JSONObject stut = (JSONObject) person.get("Result");
			JSONArray ListInfo = (JSONArray) stut.getJSONArray("ListInfo");

			for (int i = 0; i < ListInfo.length(); i++) {
				JSONObject jsonObj = ((JSONObject) ListInfo.opt(i));
				Map<String, String> map = new HashMap<String, String>();
				map.put("CityID", jsonObj.getString("CityID"));
				map.put("CityName", jsonObj.getString("CityName"));
				list.add(map);
			}

		} catch (Exception e) {
			throw e;
		}
		return list;
	}

	private List<DiscountInfo> parseHomeDiscountInfo(JSONObject person) throws Exception {
		List<DiscountInfo> list = new ArrayList<DiscountInfo>();
		try {
			JSONObject stut = (JSONObject) person.get("Result");
			JSONArray ListInfo = (JSONArray) stut.getJSONArray("ListInfo");
			JSONObject time = stut.getJSONObject("DetailInfo");
			String lastTime = time.getString("RefreshTime");
			for (int i = 0; i < ListInfo.length(); i++) {
				JSONObject jsonObj = ((JSONObject) ListInfo.opt(i));
				DiscountInfo discountInfo = new DiscountInfo();
				discountInfo.setInfoID(jsonObj.getString("InfoID"));
				discountInfo.setInfoTitle(jsonObj.getString("InfoTitle"));
				discountInfo.setInfoType(jsonObj.getString("InfoType"));
				discountInfo.setBeginTime(jsonObj.getString("BeginTime"));
				discountInfo.setEndTime(jsonObj.getString("EndTime"));
				discountInfo.setPictrueUrl(jsonObj.getString("PictureUrl"));
				discountInfo.setDistance(jsonObj.getString("Distance"));
				discountInfo.setShopName(jsonObj.getString("ShopName"));
				list.add(discountInfo);

			}

		} catch (Exception e) {
			throw e;
		}

		return list;
	}

	private List<HomeInfo> parserHome(JSONObject person) throws Exception {

		List<HomeInfo> list = new ArrayList<HomeInfo>();
		try {

			JSONObject stuts = (JSONObject) person.get("Result");
			JSONArray jsonArray = (JSONArray) stuts.getJSONArray("ListInfo");
			JSONObject time = stuts.getJSONObject("DetailInfo");

			// SharePrefenceUtil su = new SharePrefenceUtil(context,
			// Static.SP_HOMEUPDATETIME);
			// su.setHomeUpdateTime(user.getUpdateTime());

			for (int i = 0; i < jsonArray.length(); i++) {
				JSONObject item = (JSONObject) jsonArray.opt(i);
				HomeInfo homeInfo = new HomeInfo();
				homeInfo.setInfoID(item.getString("InfoID"));
				homeInfo.setInfoTitle(item.getString("InfoTitle"));
				homeInfo.setPictureUrl(item.getString("PictureUrl"));
				homeInfo.setPictureOriUrl(item.getString("PictureUrl"));
				homeInfo.setSimpleIntro(item.getString("SimpleIntro"));
				homeInfo.setIsDeleted(item.getString("IsDeleted"));
				homeInfo.setUpdateTime(item.getString("UpdateTime"));
				list.add(homeInfo);
			}

		} catch (Exception e) {
			throw e;
		}
		return list;
	}

	public class HomeInfo implements Serializable {
		private String InfoID;
		private String InfoTitle;
		private String PictureUrl;
		private String SimpleIntro;

		private String IsDeleted;
		private String UpdateTime;

		/** 完整图片路径，区分数据库图片路径 **/
		private String PictureOriUrl;

		public HomeInfo() {
			super();
		}

		public String getInfoID() {
			return InfoID;
		}

		public void setInfoID(String infoID) {
			InfoID = infoID;
		}

		public String getInfoTitle() {
			return InfoTitle;
		}

		public void setInfoTitle(String infoTitle) {
			InfoTitle = infoTitle;
		}

		public String getPictureUrl() {
			return PictureUrl;
		}

		public void setPictureUrl(String pictureUrl) {
			PictureUrl = pictureUrl;
		}

		public String getSimpleIntro() {
			return SimpleIntro;
		}

		public void setSimpleIntro(String simpleIntro) {
			SimpleIntro = simpleIntro;
		}

		public void setIsDeleted(String isDeleted) {
			IsDeleted = isDeleted;
		}

		public String getIsDeleted() {
			return IsDeleted;
		}

		public String getUpdateTime() {
			return UpdateTime;
		}

		public void setUpdateTime(String updateTime) {
			UpdateTime = updateTime;
		}

		public String getPictureOriUrl() {
			return PictureOriUrl;
		}

		public void setPictureOriUrl(String pictureOriUrl) {
			PictureOriUrl = pictureOriUrl;
		}

	}

}
